package com.bubaiwantong.algorithms1.insert;

/**
 * @author bubaiwantong
 * @date 2022/12/7 23:27
 * @description this is a class file created by bubaiwantong in 2022/12/7 23:27
 */
public class ShellSort {

    public static void main(String[] args) {
        int[] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
        ShellSort simpleSort = new ShellSort();
        simpleSort.print(arr);
        simpleSort.sort(arr);
        simpleSort.print(arr);
    }


    /**
     * 希尔排序
     *
     * @param arr
     */
    public void sort(int arr[]) {
        for (int gap = 4; gap > 0; gap /= 2) {
            for (int i = gap; i < arr.length; i++) {
                int temp = arr[i];
                int j = i;
                while (j - gap >= 0 && temp < arr[j - gap]) {
                    arr[j] = arr[j - gap];
                    j -= gap;
                }
                arr[j] = temp;
            }
        }
    }

    /**
     * 交换
     *
     * @param arr
     * @param i
     * @param j
     */
    public void swap(int arr[], int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    /**
     * 输出数组
     *
     * @param arr
     */
    public void print(int arr[]) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

}
